/**
 * 处理地区
 */
import provinceData from "../province"
import cityData from "../city"
import areaData from "../area"
import { PickerRegionResult, PickerRegionOptions } from "../../../../types/tn-picker"
import { ref } from 'vue'

export default function useRegion() {

  // 地区配置参数
  let regionOption: PickerRegionOptions = {}
  // 地区数据
  let regionValue: PickerRegionResult = {}
  // 地区列表值
  let regionColumnData = ref<string[][]>([])
  // 选中地区的index
  let regionSelectValue = ref<number[]>([])

  // 更新地区数据
  const updateRegion = (region: string[]) => {
    let regionIndex: number[] = [0, 0, 0]
    // 由于地区是由3部分组成的，所以地区数据默认省市区都是存在的
    for (let i = 0; i < 3; i++) {
      const defaultRegion = region?.[i] || null
      let index = 0
      if (defaultRegion) {
        // 省份
        if (i === 0) index = provinceData.findIndex(province => province.label === defaultRegion || province.value === defaultRegion)
        // 市级
        if (i === 1) index = cityData[regionIndex[0]].findIndex(city => city.label === defaultRegion || city.value === defaultRegion)
        // 区级
        if (i === 2) index = areaData[regionIndex[0]][regionIndex[1]].findIndex(area => area.label === defaultRegion || area.value === defaultRegion)
        regionIndex[i] = index !== -1 ? index : 0
      }
    }
    // 设置默认的地区信息
    regionValue.province = provinceData[regionIndex[0]]
    regionValue.city = cityData[regionIndex[0]][regionIndex[1]]
    regionValue.area = areaData[regionIndex[0]][regionIndex[1]][regionIndex[2]]

    generateColumnData(regionIndex)
  }
  
  // 更新地区配置数据
  const updateRegionOption = (option: PickerRegionOptions) => {
    regionOption = option
  }

  // 生成列表数据
  const generateColumnData = (regionIndex: number[]) => {
    regionColumnData.value = []
    regionSelectValue.value = []
    if (regionOption.province) {
      regionSelectValue.value.push(regionIndex[0])
      regionColumnData.value.push(provinceData.map(item => item.label))
    }
    if (regionOption.city) {
      regionSelectValue.value.push(regionIndex[1])
      regionColumnData.value.push(cityData[regionIndex[0]].map(item => item.label))
    }
    if (regionOption.area) {
      regionSelectValue.value.push(regionIndex[2])
      regionColumnData.value.push(areaData[regionIndex[0]][regionIndex[1]].map(item => item.label))
    }
  }

  // 更新选中列的数据
  const updateRegionSelectValue = (val: number[]) => {
    // 比较判断修改的是哪一列
    const index = regionSelectValue.value.findIndex((item: number, i: number) => {
      return item !== val[i]
    })
    let selectIndex = 0
    let regionIndex: number[] = [...regionSelectValue.value]
    if (regionOption.province) {
      regionIndex[0] = val[selectIndex]
      regionValue.province = provinceData[val[selectIndex++]]
    }
    if (regionOption.city) {
      regionIndex[1] = val[selectIndex]
      if (selectIndex > index) {
        regionIndex[1] = 0
        regionColumnData.value[selectIndex] = cityData[regionIndex[0]].map(item => item.label)
        regionValue.city = cityData[regionIndex[0]][0]
      } else {
        regionValue.city = cityData[regionIndex[0]][val[selectIndex]]
      }
      selectIndex++
    }
    if (regionOption.area) {
      regionIndex[2] = val[selectIndex]
      if (selectIndex > index) {
        regionIndex[2] = 0
        regionColumnData.value[selectIndex] = areaData[regionIndex[0]][regionIndex[1]].map(item => item.label)
        regionValue.area = areaData[regionIndex[0]][regionIndex[1]][0]
      } else {
        regionValue.area = areaData[regionIndex[0]][regionIndex[1]][val[selectIndex]]
      }
      
    }

    regionSelectValue.value = val.map((item: number, i: number) => {
      return i > index ? 0 : item
    })
  }

  // 获取地区结果
  const getRegionResult = (): PickerRegionResult => {
    let result: PickerRegionResult = {}
    if (regionOption.province) result.province = regionValue.province
    if (regionOption.city) result.city = regionValue.city
    if (regionOption.area) result.area = regionValue.area
    return result
  }

  return {
    regionColumnData,
    regionSelectValue,
    updateRegion,
    updateRegionOption,
    updateRegionSelectValue,
    getRegionResult
  }
}